<?php

namespace app\api\controller;

use Tencent\TLSSigAPIv2;
use app\common\controller\Api;

class Trtccontroller extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 生成 UserSig
     */
    public function genUserSig()
    {
        // 获取配置
        $config = get_addon_config('classroom');
        $sdkAppId = $config['sdkAppId'];
        $secretKey = $config['secretKey'];

        // 获取当前用户 ID
        $userId = $this->auth->id;
        // $this->success("获取成功", [
        //     "sdkAppId" => $sdkAppId,
        //     "userId" => $userId,
        // ]);
        $user = $this->auth->getUserinfo();

        // 使用官方示例代码，直接初始化 TLSSigAPIv2 类
        $api = new TLSSigAPIv2($sdkAppId, $secretKey);

        // 生成 UserSig
        $sig = $api->genUserSig($userId);


        // 输出生成的 UserSig
        $this->success("获取成功", [
            "sdkAppId" => $sdkAppId,
            "UserSig" => $sig,
            "userId" => $userId,
            "userName" => $user["nickname"],
            "avatarUrl" => $user["avatar"],
        ]);
    }
}
